class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        int dp[300][300] = { 0 };
        int ans = 0;
        for (int i = 0; i < matrix.size(); i++)
        {
            for (int j = 0; j < matrix[0].size(); j++)
            {
                if (i == 0 || j == 0)
                {
                    if (matrix[i][j] == '1') dp[i][j] = 1;

                }
                else if (matrix[i][j] == '1')
                {
                    dp[i][j] = min({ dp[i - 1][j - 1],dp[i][j - 1],dp[i - 1][j] }) + 1;
                }
                if (dp[i][j] > ans)ans = dp[i][j];
            }
        }
        return ans * ans;
    }
};